/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

/**
 *
 * @author adieffe
 */
public class Solution
{
    String name;
    Vector<Action> actionList;
    
    public Solution()
    {
        actionList = new Vector<Action>();
    }

    public void add( Action action )
    {
        actionList.add( action );
    }
    
    public void clear()
    {
        actionList.clear();
    }

    public Vector<Action> getActionList()
    {
        return( actionList );
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public String getName()
    {
        return( name );
    }

    public boolean writeToFile(String fileName)
    {
        
        FileWriter fstream = null;
        BufferedWriter out = null;
        try
        {
            fstream = new FileWriter(fileName);
            out = new BufferedWriter(fstream);
            out.write(name + "\n");
                       //  System.out.print(name + "\n");
            out.write(actionList.size() + "\n");
                          //System.out.print(itemList.size() + "\n");
            Action action;
            String save;
            for (int i = 0; i<actionList.size();i++)
            {
                save = "";
                action = actionList.get(i);
                if (action instanceof CommunicationAction)
                    save = "C";
                else if (action instanceof MovementAction)
                    save = "M";
                else if (action instanceof TransferAction)
                    save = "T";
                else if (action instanceof CreationAction)
                    save = "E";
                else if (action instanceof RemoveAction)
                    save = "R";
                else if (action instanceof CarConditionAction)
                    save = "D";
                else
                    save = "";

                if (!save.equals(""))
                {
                    save += action.toSaveString();
                    out.write(save + "\n");
                    //System.out.print(save + "\n");
                }
            }


        } catch (IOException ex) {
            return false;
        } finally {
            try {
                out.close();
                fstream.close();
            } catch (IOException ex) {
               
            }
        }
        

        return true;
    }


    public boolean loadFromFile(String fileName, Vector<Item> itemList)
    {
        try
        {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(fileName);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            name = br.readLine();

            strLine = br.readLine();
            int length = Integer.parseInt(strLine);
            actionList = new Vector<Action>();
            Action action;
            for (int i = 0; i < length; i++)
            {
                //System.out.println("Loop: " + i);
                strLine = br.readLine();
                action = null;
                //System.out.println("Type: " + strLine.substring(0, 1));
                if (strLine.startsWith("C"))
                    action = new CommunicationAction(strLine.substring(1), itemList);
                else if (strLine.startsWith("M"))
                {
                    action = new MovementAction(strLine.substring(1), itemList);

                }
                else if (strLine.startsWith("T"))
                    action = new TransferAction(strLine.substring(1), itemList);
                else if (strLine.startsWith("E"))
                    action = new CreationAction(strLine.substring(1), itemList);
                else if (strLine.startsWith("R"))
                    action = new RemoveAction(strLine.substring(1), itemList);
                else if (strLine.startsWith("D"))
                    action = new CarConditionAction(strLine.substring(1), itemList);
                

                if (action != null)
                    actionList.add(action);

            }
        } catch (IOException ex) {
            return false;
        }

        return true;
    }
}
